<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?>
<!--返回错误信息-->
<?php echo validation_errors(); ?>
<!DOCTYPE html>
<html lang="en">

<head>
    <?php
    $title = [
        'title' => '统计分析管理/采购质量统计汇总',
    ];
    $this->load->view('/admin/common/header', $title);
    ?>
</head>

<body data-type="widgets">
<script src="/assets/js/theme.js"></script>
<div class="am-g tpl-g">
    <!--    头部   侧边菜单-->
    <?php $this->load->view('/admin/common/bodyLeft'); ?>
    <script type="text/javascript">
        $(".js-sidebar-nav>li:nth-child(11)>ul, .js-sidebar-nav>li:nth-child(11)>ul>li:nth-child(2)>ul").show(80);
        $(".js-sidebar-nav>li:nth-child(11)>a>span, .js-sidebar-nav>li:nth-child(11)>ul>li:nth-child(2)>a>span").addClass("sidebar-nav-sub-ico-rotate");
        $(".js-sidebar-nav>li:nth-child(11)>ul>li:nth-child(2)>a, .js-sidebar-nav>li:nth-child(11)>ul>li:nth-child(2)>ul>li:nth-child(2)>a").addClass("active");
    </script>
    <!-- 内容区域 -->
    <div class="tpl-content-wrapper statement">

        <div class="row-content am-cf">
            <div class="row">
                <div class="am-u-sm-12 am-u-md-12 am-u-lg-12">
                    <div class="widget am-cf">
                        <div class="widget-head am-cf">
                            <div class="widget-title  am-cf">按商品类别统计</div>
                        </div>
                        <div class="widget-body  am-fr">
                            <div class="am-u-sm-6 am-u-md-6 am-u-lg-6">
                                <div class="am-btn-toolbar">
                                    <div class="am-g">
                                        <div class="am-u-sm-12 am-u-md-6 am-form-group">
                                            <button type="button" class="am-btn am-btn-default am-margin-right"
                                                    id="my-start">开始日期
                                            </button>
                                            <span id="my-startDate"></span>
                                        </div>
                                        <div class="am-u-sm-12 am-u-md-6 am-form-group">
                                            <button type="button" class="am-btn am-btn-default am-margin-right"
                                                    id="my-end">结束日期
                                            </button>
                                            <span id="my-endDate"></span>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="am-u-sm-6 am-u-md-6 am-u-lg-6">

<!--                                <div class="am-u-sm-12 am-u-md-12 am-u-lg-6">-->
<!--                                    <div class="am-form-group am-input-group am-input-group-sm tpl-form-border-form cl-p">-->
<!--                                        <!--                                    <form action="/admin/user/search" method="get">-->
<!--                                        <input type="text" class="am-form-field" name="search" id="search">-->
<!--                                        <span class="am-input-group-btn">-->
<!--                                            <a href="javascript:void(0)" onclick="search123()"-->
<!--                                               class="am-btn  am-btn-default am-btn-success tpl-table-list-field am-icon-search">搜索</a>-->
<!--                                        </span>-->
<!--                                        <!--                                    </form>-->
<!--                                    </div>-->
<!--                                </div>-->

                            </div>
                            <div class="am-u-sm-12 am-form-group ">
                                <div class="am-btn-toolbar am-u-sm-12">
                                    <div class=" am-btn-group-xs">
                                        <?php
                                        $type = isset($_GET['type']) ? $_GET['type'] : 'returnOrder';
                                        ?>
                                        <a href="javascript:void(0);" onclick="returnOrder()"
                                           class="am-btn   <?php if ($type == 'returnOrder') {
                                               echo 'am-btn-primary';
                                           } else {
                                               echo 'am-btn-default';
                                           } ?> ">退货统计</a>
                                        <a href="javascript:void(0);" onclick="barterOrder()"
                                           class="am-btn  <?php if ($type == 'barterOrder') {
                                               echo 'am-btn-primary ';
                                           } else {
                                               echo 'am-btn-default';
                                           } ?> ">换货统计</a>
                                    </div>
                                </div>
                            </div>
                            <div class="am-u-sm-12 am-form-group ">
                                <div class="am-btn-toolbar am-u-sm-12">
                                    <div class=" am-btn-group-xs">
                                        <a href="javascript:void(0);" onclick="" class="am-btn am-btn-primary"><span
                                                    class="am-icon-download"></span> 导出</a>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="am-u-sm-12 ul-table">
                            <table width="100%" class="am-table am-table-compact table-hover tpl-table-black"
                                   id="example-r">
                                <thead>
                                <?php
                                if(isset($thlist) && $thlist){
                                    echo '<tr>
                                    <td>序号</td>
                                    <td>商品编码</td>
                                    <td>商品名称</td>
                                    <td>所属供应商</td>
                                    <td>采购商品数量</td>
                                    <td>退货商品数量</td>
                                    <td>退货率</td>
                                    </tr>';
                                }else if(isset($hhlist) && $hhlist){
                                    echo '<tr>
                                <td>序号</td>
                                <td>商品编码</td>
                                <td>商品名称</td>
                                <td>所属供应商</td>
                                <td>采购商品数量</td>
                                <td>换货商品数量</td>
                                <td>换货率</td>
                            </tr>';
                                }
                                ?>
                                </thead>

                                <tbody class="choose-box">

                                <?php
                            if (isset($thlist) && $thlist) {
                                //2物料包、3书籍、4机器耗材、9打印机租赁、8供应商商品
                                foreach ($thlist as $k=>$v) {
                                    $dataId = $v['ckId'] ? $v['ckId'] : $v['id'];

                                    echo '<tr>';
                                    echo'<td date-id="' . $dataId . '">' . $v['id'] . '</td>';
                                    if(substr($v['product_no'],0,3) == 'ist'){
                                        echo '<td><a href="/admin/goods/editView?id='.$v['id'].'">' . $v['product_no'] . '</a></td>';
                                    }else{
                                        echo '<td>' . $v['product_no'] . '</td>';
                                    }
                                    $purchaseNum = isset($v['purchaseNum']) ? $v['purchaseNum'] : 1;

                                      echo '<td>' . $v['name'] . '</td>
                                            <td>' . $v['supplier'] . '</td>
                                            <td>' . $purchaseNum . '</td>
                                            <td>' . $v['quantity'] . '</td>
                                            <td>' . round(($v['quantity'] / $purchaseNum) * 100,2).'%' . '</td>';


                                     echo    '</ul>';
                                }
                            }else if(isset($hhlist) && $hhlist){
                                foreach ($hhlist as $k2=>$v2) {
                                    $dataId = $v2['ckId'] ? $v2['ckId'] : $v2['id'];

                                    echo '<tr>';
                                    echo'<td date-id="' . $dataId . '">' . $v2['id'] . '</td>';
                                    if(substr($v2['product_no'],0,3) == 'ist'){
                                        echo '<td><a href="/admin/goods/editView?id='.$v2['id'].'">' . $v2['product_no'] . '</a></td>';
                                    }else{
                                        echo '<td>' . $v2['product_no'] . '</td>';
                                    }

                                    $purchaseNum = isset($v2['purchaseNum']) ? $v2['purchaseNum'] : 0;


                                    echo '<td>' . $v2['name'] . '</td>
                                            <td>' . $v2['supplier'] . '</td>
                                            <td>' . $purchaseNum . '</td>
                                            <td>' . $v2['quantity'] . '</td>';
                                    if($purchaseNum){
                                        echo '<td>' . round(($v2['quantity'] / $purchaseNum) * 100,2).'%' . '</td>';
                                    }else {
                                        echo '<td>0</td>';

                                    }

                                    echo    '</tr>';
                                }
                            }
                            ?>
                        </div>
                        <div class="am-u-lg-12">
                            <div class="am-u-sm-centered">
                                <ul class="am-pagination am-pagination-centered" id="paging">

                                </ul>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script src="/assets/js/amazeui.min.js"></script>
<script src="/assets/js/amazeui.datatables.min.js"></script>
<script src="/assets/js/dataTables.responsive.min.js"></script>
<script src="/assets/js/app.js"></script>
<script src="/assets/js/page.js"></script>
<script>
    $(function () {

        $(document).on("click", ".specification-btn", function () {
            var thisTr = $(this).parents(".gradeX");                        //点击的本行tr
            var productId = thisTr.children("td:first-child").data("id");   //传给后台的商品ID
            console.log(productId)

            var specificationBox = $(".specification-box");

//        specificationBox.empty();
            $.ajax({
                url: "/api/goods/getGoodsIdByData",
                data: {
                    productId: productId
                },
                dataType: "json",
                type: "post",
                success: function (data) {
//                console.log(data);
                    var tr = "";
                    for (var i = 0; i < data.length; i++) {
                        tr += '<tr>'
                            + '<td>' + data[i].specification_group_names + '</td>'
                            + '<td>' + data[i].group_no + '</td>'
                            + '</tr>';
                    }

                    specificationBox.html(tr);
                },
                error: function (data) {
                    console.log(data);
                }
            });
        });

        var startDate = new Date(2014, 11, 20);
        var endDate = new Date(2014, 11, 25);
        var $alert = $('#my-alert');
        $('#my-start').datepicker().on('changeDate.datepicker.amui', function (event) {
            if (event.date.valueOf() > endDate.valueOf()) {
                $alert.find('p').text('开始日期应小于结束日期！').end().show();
            } else {
                $alert.hide();
                startDate = new Date(event.date);
                $('#my-startDate').text($('#my-start').data('date'));
            }
            $(this).datepicker('close');
        });

        $('#my-end').datepicker().on('changeDate.datepicker.amui', function (event) {
            if (event.date.valueOf() < startDate.valueOf()) {
                $alert.find('p').text('结束日期应大于开始日期！').end().show();
            } else {
                $alert.hide();
                endDate = new Date(event.date);
                $('#my-endDate').text($('#my-end').data('date'));
            }
            $(this).datepicker('close');
        });
    });
</script>

<script>
    function GetQueryString(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if (r != null)return unescape(r[2]);
        return null;
    }
    function barterOrder() {
        var search = GetQueryString('search');
        // $(".am-btn-primary").removeClass("am-btn-primary");
        // $(this).addClass("am-btn-primary");

        if (search == null) {
            window.location.href = '?page=1' + '&type=barterOrder'
        } else {
            window.location.href = '?page=1' + '&search=' + search + '&type=barterOrder'
        }
    }
    function returnOrder() {
        var search = GetQueryString('search');
        if (search == null) {
            window.location.href = '?page=1' + '&type=returnOrder'
        } else {
            window.location.href = '?page=1' + '&search=' + search + '&type=returnOrder'

        }
    }

</script>

<script>
//    $(document).ready(function () {
//        //设置分页信息
//        var type = "<?php //echo isset($_GET['type']) ? $_GET['type'] : 'returnOrder' ?>//";
//        var pageOptions = {
//            AllowPaging: true,
//            PageIndex: <?php //echo $_GET['page'] ?>//,      //设置当前页码
//            PageSize: <?php //echo $pageNum ?>//,       //设置分页大小
//            RecordCount: <?php //echo $totalNum ?>//,  //设置数据总数
//            // TotalPage: ,      //设置总页数
//            showPageCount: 4,
//            onPageClick: function (pageIndex) {
//                var page = parseInt(pageIndex);
//                window.location.href = "?page=" + page + "&type=" + type;
//            }
//        };
//        //初始化分页栏
//        $('#paging').pageBar(pageOptions);
//    })
</script>
</body>

</html>